home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
- Subject: Re: Access carry flag from C
- Date: 28 Feb 1996 08:21:44 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4h1veoINNlns@anvil.ugrad.cs.ubc.ca>
- References: <Dn1C9z.DGv.0.net@indra.com> <824853272snz@genesis.demon.co.uk> <4gqj0d$d6p@airdmhor.gen.nz> <825377932snz@genesis.demon.co.uk>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <825377932snz@genesis.demon.co.uk>,
- Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
- >In article <4gqj0d$d6p@airdmhor.gen.nz>
- > gumboot@airdmhor.gen.nz "Simon Hosie" writes:
- >
- >>Lawrence Kirby:
- >>> It certainly can be done portably although not with the efficiency that
- >>> a platform-specific solution is likely to give you.
- >>
- >>> int j, k;
- >>
- >>> ...
- >>
- >>> if ((j >= 0) ? (k > INT_MAX-j) : (k < INT_MIN-j))
- >> ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
- >> Won't they both be evaluated?
- >
- >No, given:
- >
- > (expr1) ? expr2 : (expr3)
- >
- >the (ANSI) C language guarantees that expr1 will be evaluated and followed
- >by a sequence point. If expr1 evaluates to a value that compares unequal
- >to zero then expr2 is evaluated, otherwise expr3 is evaluated. Under no
- >circumstances (except undefined behaviour caused by something else in the
- >program, where anything can happen) can both expr2 and expr3 be evaluated.
- >
- >You probably noticed that I didn't put parentheses around expr2. Can you
- >see why they are necessary around expr1 and expr3 but not here? (This does
- >test understanding of expression parsing quite deeply).
-
- Not really. The ?: has a very low precedence, but there are expressions that
- have an even lower precedence; namely assignment expressions and comma
- expressions.
-
- If (expr1) was written without the brackets, and contained a comma or
- assignment expression, then the rest of the statement would get interpreted as
- just the rightmost branch of the comma list, or just the right side of the
- assignment expression.
-
- Same goes for expr3, execpt s/right/left/
-
- The middle expression does not need this because the parser is already in the
- middle of the production. It has seen the ?, so it knows it has the prefix of
- a conditional expression. In effect, the ? and : symbols already act
- analogously to parentheses.
- --
-
-